package com.hb.mybatis.typehandler;

import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.hb.encryptor.IEncryptor;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

/**
 * @author hubin
 * @description
 * @createTime 2024/3/11 15:35
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(LocalDate.class)
public class BirthdayTypeHandler extends BaseTypeHandler {

    @Autowired
    private IEncryptor encryptor;

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        if (parameter == null) {
            return;
        }
        String dateStr = parameter.toString();
        String key = "1234567812345678";
        byte[] bytes = key.getBytes();
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, bytes);
        String encryptContent = aes.encryptBase64(dateStr);
        ps.setString(i, "pier_rois_" + encryptContent);
    }

    @Override
    public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String cipher = rs.getString(columnName);
        if (!StringUtils.hasLength(cipher)) {
            return null;
        }
        String cipherPrefix = cipher.substring(0, 10);
        if (!cipherPrefix.equals("pier_rois_")) {
            return LocalDate.parse(cipher);
        }
        String decryptContent = encryptor.decrypt(cipher.substring(10), "1234567812345678");
        return LocalDate.parse(decryptContent);
    }

    @Override
    public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String cipher = rs.getString(columnIndex);
        if (!StringUtils.hasLength(cipher)) {
            return null;
        }
        String cipherPrefix = cipher.substring(0, 10);
        if (!cipherPrefix.equals("pier_rois_")) {
            return LocalDate.parse(cipher);
        }
        String decryptContent = encryptor.decrypt(cipher.substring(10), "1234567812345678");
        return LocalDate.parse(decryptContent);
    }

    @Override
    public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String cipher = cs.getString(columnIndex);
        if (!StringUtils.hasLength(cipher)) {
            return null;
        }
        String cipherPrefix = cipher.substring(0, 10);
        if (!cipherPrefix.equals("pier_rois_")) {
            return LocalDate.parse(cipher);
        }
        String decryptContent = encryptor.decrypt(cipher.substring(10), "1234567812345678");
        return LocalDate.parse(decryptContent);
    }
}
